package Service.FileService.Imp;

import Dao.DaoFactory;
import Dao.MsgAndEmail.MessageDao;
import Dao.Process.ReportDetailDao;
import Dao.Process.StageCheckDao;
import Dao.Role.StuInfoDao;
import Dao.Role.TeacherDao;
import Entity.MyTableData;
import Entity.Process.StageCheck;
import Entity.Role.Student;
import Entity.Role.Teacher;
import Service.FileService.FileService;
import Util.DBUtil;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class FileServiceImp implements FileService {
    private Connection conn;
    private ReportDetailDao reportDetailDao;
    private StageCheckDao stageCheckDao;
    private StuInfoDao stuInfoDao;
    private TeacherDao teacherDao;
    private MessageDao messageDao;

    public FileServiceImp() {
        conn = DBUtil.getConnByDruid();
        reportDetailDao = DaoFactory.getInstance("reportDetailDao");
        stageCheckDao = DaoFactory.getInstance("stageCheckDao");
        stuInfoDao = DaoFactory.getInstance("stuInfoDao");
        teacherDao = DaoFactory.getInstance("teacherDao");
        messageDao = DaoFactory.getInstance("messageDao");
    }


    @Override
    public Boolean updateReportFile(String SNO, String fileName, String fileUrl, String type, Integer reportId) {
        Boolean flag = reportDetailDao.updateReportFile(SNO, fileName, fileUrl, type, reportId, conn) == 1;
        DBUtil.closeConn(conn);
        return flag;
    }


    @Override
    public Boolean updateStageCheck(String SNO, String fileName, String fileUrl, String state) {
        //提交初稿 定稿 终稿
        //先查询判断是否存
        boolean flag;
        Student student = stuInfoDao.getStuByNO(SNO, conn).get(0);
        StageCheck stageCheck = stageCheckDao.getStageCheckBySNOAndPhase(SNO, state, conn);
        if (stageCheck == null || "".equals(stageCheck.getSno()) || stageCheck.getSno() == null) {
            stageCheck = new StageCheck();
            stageCheck.setThisSum(3);
            stageCheck.setUserSum(9);
            stageCheck.setSno(SNO);
            stageCheck.setSFileName(fileName);
            stageCheck.setSFileUrl(fileUrl);
            stageCheck.setState("审核中");
            stageCheck.setPhase(state);
            stageCheck.setTno(student.getTNO());
            flag = stageCheckDao.insertState(stageCheck, conn) == 1;
        } else {
            stageCheck.setSno(SNO);
            stageCheck.setSFileName(fileName);
            stageCheck.setSFileUrl(fileUrl);
            stageCheck.setState("审核中");
            stageCheck.setPhase(state);
            stageCheck.setIsPass(null);
            stageCheck.setOpinion(null);
            flag = stageCheckDao.updateReportFile(stageCheck, conn) == 1;
        }
        DBUtil.closeConn(conn);
        return flag;
    }

    @Override
    public MyTableData importStudent(ArrayList<ArrayList<String>> arrayLists) {
        ArrayList<Student> students = new ArrayList<>();
        //先对读入的数据进行解析 枚举
        // [ [ a,b,c] , [a,b,c] , [a,b,c] , [a,b,c] , [a,b,c] ]
        ArrayList<String> title = arrayLists.get(0); // 获取读取到的标题列
        for (int i = 1; i < arrayLists.size(); i++) {  //循环大列表
            Student student = new Student();    //大循环的每一个属性 就是 一个学生的信息
            for (int j = 0; j < arrayLists.get(i).size(); j++) {
                switch (title.get(j)) {
                    case "学号":
                        student.setSNO(arrayLists.get(i).get(j));
                        break;
                    case "姓名":
                        student.setName(arrayLists.get(i).get(j));
                        break;
                    case "性别":
                        student.setSex(arrayLists.get(i).get(j));
                        break;
                    case "电话":
                        student.setTel(arrayLists.get(i).get(j));
                        break;
                    case "电子邮箱":
                        student.setEmail(arrayLists.get(i).get(j));
                        break;
                    case "学院名称":
                        student.setCollName(arrayLists.get(i).get(j));
                        break;
                    case "班级":
                        student.setClasses(arrayLists.get(i).get(j));
                        break;
                    case "专业名称":
                        student.setMajorName(arrayLists.get(i).get(j));
                        break;
                    default:
                        break;
                }
            }
            student.setPassword(getRandomPass()); //设置生成的随机密码
            students.add(student);
        }
        List<Student> success = new ArrayList<>(); //保存添加成功的学生
        for (Student student : students) {
            //往数据库插入解析的数据
            // 方法一：每次插入数据前先查询是否存在数据  如果存在则修改 如果不存在则删除 此方法效率较低
            // 方法二：数据库将学号设置唯一约束  直接插入数据  如果产生异常 则说明里面已经存在数据 只需要在异常中执行更新数据操作 效率比第一种方法高
            if (stuInfoDao.importImpStu(student, conn) == 1) {
                success.add(student);
            }
        }
        DBUtil.closeConn(conn);
        return new MyTableData(0, "", success.size(), success);
    }


    @Override
    public MyTableData importTeacher(ArrayList<ArrayList<String>> arrayLists) {
        ArrayList<Teacher> teachers = new ArrayList<>();
        //先对读入的数据进行解析 枚举
        // [ [ a,b,c] , [a,b,c] , [a,b,c] , [a,b,c] , [a,b,c] ]
        ArrayList<String> title = arrayLists.get(0); // 获取读取到的标题列
        for (int i = 1; i < arrayLists.size(); i++) {  //循环大列表
            Teacher teacher = new Teacher();    //大循环的每一个属性 就是 一个老师的信息
            for (int j = 0; j < arrayLists.get(i).size(); j++) {
                switch (title.get(j)) {
                    case "工号":
                        teacher.setTNO(arrayLists.get(i).get(j));
                        break;
                    case "姓名":
                        teacher.setName(arrayLists.get(i).get(j));
                        break;
                    case "性别":
                        teacher.setSex(arrayLists.get(i).get(j));
                        break;
                    case "职称":
                        teacher.setPost(arrayLists.get(i).get(j));
                        break;
                    case "电话":
                        teacher.setTel(arrayLists.get(i).get(j));
                        break;
                    case "电子邮箱":
                        teacher.setEmail(arrayLists.get(i).get(j));
                        break;
                    case "学院":
                        teacher.setCollegeId(arrayLists.get(i).get(j));
                        break;
                    case "部门":
                        teacher.setDepartment(arrayLists.get(i).get(j));
                        break;
                    default:
                        break;
                }
            }
            teacher.setPassword(getRandomPass()); //设置生成的随机密码
            teachers.add(teacher);
        }
        List<Teacher> success = new ArrayList<>(); //保存添加成功的老师
        for (Teacher teacher : teachers) {
            if (teacherDao.importImpTeacher(teacher, conn) == 1) {
                success.add(teacher);
            }
        }
        DBUtil.closeConn(conn);
        return new MyTableData(0, "", success.size(), success);
    }

    @Override
    public Boolean updateMsgFileById(Integer id, String fileName, String savaPath) {
        Boolean flag = messageDao.updateFile(id, fileName, savaPath, conn) == 1;
        DBUtil.closeConn(conn);
        return flag;
    }


    /**
     * 随机生产9位数密码
     *
     * @return 随机生产的密码
     */
    public String getRandomPass() {
        //生成密码
        Random random = new Random();
        StringBuilder password = new StringBuilder();
        for (int i = 0; i < 8; i++) {
            // 1、2随机生成整数  如果 1就生成字符 2则生成数字
            String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
            //生成字符
            if (charOrNum.equals("char")) {
                // 判断是生成大写还是小写
                int choice = random.nextInt(2) == 0 ? 65 : 97;
                password.append((char) (choice + random.nextInt(26)));
            } else { //生成数字
                password.append(random.nextInt(10));
            }
        }
        return String.valueOf(password);
    }
}
